package com.atguigu.tingshu.user.paid.impl;

import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.model.user.UserInfo;
import com.atguigu.tingshu.model.user.UserVipService;
import com.atguigu.tingshu.model.user.VipServiceConfig;
import com.atguigu.tingshu.user.mapper.UserInfoMapper;
import com.atguigu.tingshu.user.mapper.UserVipServiceMapper;
import com.atguigu.tingshu.user.mapper.VipServiceConfigMapper;
import com.atguigu.tingshu.user.paid.PaidRecordStrategy;
import com.atguigu.tingshu.vo.user.UserPaidRecordVo;
import lombok.extern.slf4j.Slf4j;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component(value = SystemConstant.ORDER_ITEM_TYPE_VIP)//1003
@Slf4j
public class VipServiceStrategy implements PaidRecordStrategy {
    @Autowired
    private UserVipServiceMapper userVipServiceMapper;
    @Autowired
    private VipServiceConfigMapper vipServiceConfigMapper;
    @Autowired
    private UserInfoMapper userInfoMapper;
    @Override
    public void savePaidRecords(UserPaidRecordVo userPaidRecordVo) {
        //用户表记录vip状态
        //查找购买的vip产品： 使用月数+当前时间
        VipServiceConfig vipServiceConfig = vipServiceConfigMapper.selectById(userPaidRecordVo.getItemIdList().get(0));
        Date vipExpireTime = new DateTime().plusMonths(vipServiceConfig.getServiceMonth()).toDate();
        //1、查询用户表vip状态
        UserInfo userInfo = userInfoMapper.selectById(userPaidRecordVo.getUserId());

        //vip购买记录
        UserVipService userVipService = new UserVipService();
        if(userInfo.getIsVip().intValue()!=1){
            //如果不是vip：修改用户为vip并设置过期时间  再保存vip购买记录
            userInfo.setIsVip(1);
            userInfo.setVipExpireTime(vipExpireTime);

            userVipService.setStartTime(new Date());
            userVipService.setExpireTime(new DateTime().plusMonths(vipServiceConfig.getServiceMonth()).toDate());

        }else{
            //如果是vip： 修改用户vip过期时间  再保存vip购买记录
            if(!userInfo.getVipExpireTime().before(new Date())){
                vipExpireTime = new DateTime(userInfo.getVipExpireTime())
                        .plusMonths(vipServiceConfig.getServiceMonth()).toDate();

                userVipService.setStartTime(userInfo.getVipExpireTime());
                userVipService.setExpireTime(new DateTime(userVipService.getStartTime()).plusMonths(vipServiceConfig.getServiceMonth()).toDate());

            }else{
                userVipService.setStartTime(new Date());
                userVipService.setExpireTime(new DateTime().plusMonths(vipServiceConfig.getServiceMonth()).toDate());
            }
            userInfo.setIsVip(1);
            userInfo.setVipExpireTime(vipExpireTime);
        }
        userInfoMapper.updateById(userInfo);

        userVipService.setUserId(userPaidRecordVo.getUserId());
        userVipService.setOrderNo(userPaidRecordVo.getOrderNo());
        userVipServiceMapper.insert(userVipService);
    }
}
